#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/named_sync_slaves,v 1.1 2012/11/27 07:50:51 yajin Exp yajin $

# Author: yang.jin@ebay.com
# this script calls the Perl script "named_sync_slaves.pl" and synchronizes IT DNS slaves. 

print_usage()
{

echo -e '

named_sync_slaves - generate configuration for slave zones and synchronize the configuration to slave DNS servers.

Usage:
        named_sync_slaves -s sites [-d slave-servers]

        "sites" could be "all", otherwise, one or combination of the following:
                its
                mp
                base
                misc
                qa
                nodb

        "slave-servers" could be "all", otherwise, one or combination of the hosts defined on /etc/named/IT_DNS_SERVERS.

Examples:

  Propagate changes to misc.zones and nodb.zones onto sjcitdns03 and sjcitdns04 -
        named_sync_slaves -s "misc nodb" -d "sjcitdns03 sjcitdns04"

  Propagate changes to its.zones onto all slave servers -
        named_sync_slaves -s its -d all
    or,
        named_sync_slaves -s its 
'
        
}

DNS_SERVERS_LIST="/etc/named/IT_DNS_SERVERS"
SYNC_SLAVES_TOOL="/usr/local/sbin/named_sync_slaves.pl"
ISPRI="/usr/local/sbin/is_primaster"

if [ "X$(uname -s)" == "XSunOS" ]; then
	GREP="/usr/xpg4/bin/grep"
	SED="/usr/xpg4/bin/sed"
else
	GREP="/bin/grep"
	SED="/bin/sed"
fi


# this tool is suppposed to be executed on the primary DNS master
if [ ! -x $ISPRI ]; then
	echo "$ISPRI does not exist, or, is not executable." >&2
	exit 2
fi

$ISPRI
if [ $? -ne 0 ]; then 
	echo "This host is not the primary DNS master."  >&2
	exit 2
fi


# synatx of this script is supposed to be the same with the one of "*.pl".

len=$#
if [ $len -ne 2 ] && [ $len -ne 4 ]; then print_usage; exit 1; fi

option1=$1
para1=$2
option2=$3
para2=$4 

if [ $len -eq 2 ]; then option2="-d"; para2="all"; fi

if [ "$option1" != "-s" ] || [ "$option2" != "-d" ]; then print_usage; exit 1; fi

if [ "$para1" == "all" ]; then 
	sites="its mp base misc qa nodb"
else
	sites="$para1"
fi


if [ "$para2" == "all" ]; then
	if [ ! -f $DNS_SERVERS_LIST ]; then
		echo -e "The configuration file \"$DNS_SERVERS_LIST\" does not exist." >&2
		exit 2
	fi
	. $DNS_SERVERS_LIST
	export DNS_SLAVES 
else
	DNS_SLAVES="$para2"; export DNS_SLAVES
fi


if [ ! -x $SYNC_SLAVES_TOOL ]; then
        echo -e "$SYNC_SLAVES_TOOL does not exist or is not executable."
        exit 2
fi

# echo "$SYNC_SLAVES_TOOL -s \"$sites\" -d \"$DNS_SLAVES\""

$SYNC_SLAVES_TOOL -s "$sites" -d "$DNS_SLAVES"
